<template>
	<view class="padding-30">
		<view class="padding-30 bg-fff border-radius20 margin-bottom30">
			<view class="padding-bottom30 f-divider1 margin-bottom30">
				<view class="flex-between">
					<view class="fontSize-15 color-000 margin-right20">是否抽烟</view>
					<view class="flex-alignItems-center" @click="smokeShow = true">
						<view class="fontSize-15 color-000 margin-right10" v-if="smokeName">{{smokeName}}</view>
						<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
						<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
					</view>
				</view>
				<view class="flex-alignItems-center input-demo-item" v-if="false">
					<view class="fontSize-15 color-000">抽烟频率</view>
					<input class="input-item1" type="text" v-model="name" placeholder="请输入" />
					<view class="fontSize-15 color-000">支</view>
				</view>
			</view>
			<view class="padding-bottom30 f-divider1 margin-bottom30">
				<view class="flex-between">
					<view class="fontSize-15 color-000 margin-right20">是否饮酒</view>
					<view class="flex-alignItems-center" @click="drinkShow = true">
						<view class="fontSize-15 color-000 margin-right10" v-if="drinkName">{{drinkName}}</view>
						<view class="fontSize-15 color-999 margin-right10" v-else>待完善</view>
						<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
					</view>
				</view>
				<view class="flex-alignItems-center input-demo-item margin-bottom30" v-if="false">
					<view class="fontSize-15 color-000">哪类酒品为主</view>
					<input class="input-item1" type="text" v-model="name" placeholder="请输入" />
					<view class="fontSize-15 color-000">支</view>
				</view>
				<view class="flex-alignItems-center input-demo-item" v-if="false">
					<view class="fontSize-15 color-000">饮酒量</view>
					<input class="input-item1" type="text" v-model="name" placeholder="请输入" />
					<view class="fontSize-15 color-000">两</view>
				</view>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">口味偏好</view>
				<view class="flex-alignItems-center" @click="tasteShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="tasteName">{{tasteName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>待完善</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
			<view class="padding-bottom30 f-divider1 margin-bottom30">
				<view class="flex-between">
					<view class="fontSize-15 color-000 margin-right20">运动习惯(每周)</view>
					<view class="flex-alignItems-center" @click="exerciseShow = true">
						<view class="fontSize-15 color-000 margin-right10" v-if="exerciseName">{{exerciseName}}</view>
						<view class="fontSize-15 color-999 margin-right10" v-else>待完善</view>
						<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
					</view>
				</view>
				<view class="flex-alignItems-center input-demo-item" v-if="false">
					<view class="fontSize-15 color-000">每次运动时长</view>
					<input class="input-item1" type="text" v-model="name" placeholder="请输入" />
					<view class="fontSize-15 color-000">分</view>
				</view>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">睡眠时长(每天)</view>
				<view class="flex-alignItems-center" @click="sleepShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="sleepName">{{sleepName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>待完善</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
		</view>
		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom padding-30 bg-fff">
			<view class="btn50-0165FD-15ffffff flex-center" @click="SubmitEvent">
				保存
			</view>
		</view>
		
		<!-- 抽烟 -->
		<u-picker :show="smokeShow" :columns="smokeColumns" @cancel="smokeCancel" @confirm="smokeConfirm"></u-picker>
		<!-- 饮酒 -->
		<u-picker :show="drinkShow" :columns="smokeColumns" @cancel="drinkCancel" @confirm="drinkConfirm"></u-picker>
		<!-- 口味偏好 -->
		<u-picker :show="tasteShow" :columns="tasteColumns" @cancel="tasteCancel" @confirm="tasteConfirm"></u-picker>
		<!--  运动习惯 -->
		<u-picker :show="exerciseShow" :columns="exerciseColumn" @cancel="exerciseCancel" @confirm="exerciseConfirm"></u-picker>
		<!--  睡眠时长(每天) -->
		<u-picker :show="sleepShow" :columns="sleepColumn" @cancel="sleepCancel" @confirm="sleepConfirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				sick_id:'',
				// 抽烟
				smokeShow:false,
				smokeColumns:[],
				smokeName:'',
				// 饮酒
				drinkShow:false,
				drinkColumns:[],
				drinkName:'',
				// 口味偏好
				tasteShow:false,
				tasteColumns:[],
				tasteName:'',
				// 运动习惯
				exerciseShow:false,
				exerciseColumn:[],
				exerciseName:'',
				// 睡眠时长(每天)
				sleepShow:false,
				sleepColumn:[],
				sleepName:'',
				code:'',
				live_id:''
			}
		},
		onLoad(e) {
			if(e.code){
				this.sick_id = uni.getStorageSync('patlient-item').id
				this.live_id = e.id
				this.code = e.code
				this.getLiveDetail()
				this.getConfig()
			}else{
				if(uni.getStorageSync('patlient-item')){
					this.sick_id = uni.getStorageSync('patlient-item').id
					this.getConfig()
				}
			}
		},
		methods:{
			// 获取生活方式详情
			async getLiveDetail(){
				let res = await this.$http.post(this.$api.live_detail,{sick_id:this.sick_id},{token:true})
				if(res.code ==1){
					this.smokeName = res.data.is_smoking
					this.drinkName = res.data.is_tipple
					this.tasteName = res.data.taste
					this.exerciseName = res.data.movement
					this.sleepName = res.data.sleep
				}
			},
			// 提交
			async SubmitEvent(){
				if(!this.smokeName) return this.$tools.toast('请选择是否抽烟')
				if(!this.drinkName) return this.$tools.toast('请选择是否饮酒')
				if(!this.tasteName) return this.$tools.toast('请选择口味')
				if(!this.exerciseName) return this.$tools.toast('请选择运动习惯')
				if(!this.sleepName) return this.$tools.toast('请选择睡眠时长')
				let form = {
					sick_id:this.sick_id,
					is_smoking:this.smokeName,
					is_tipple:this.drinkName,
					taste:this.tasteName,
					movement:this.exerciseName,
					sleep:this.sleepName
				}
				if(this.code == '生活方式编辑'){
					form.id = this.live_id
					let res = await this.$http.post(this.$api.live_edit,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('修改成功')
						this.$tools.navigateBack(1,200)
					}
				}else{
					let res = await this.$http.post(this.$api.live_add,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('添加成功')
						this.$tools.navigateBack(1,200)
					}
				}
			},
			// 确认睡眠选择
			sleepConfirm(e){
				this.sleepName = e.value[0]
				this.sleepShow = false
			},
			// 取消睡眠选择
			sleepCancel(){
				this.sleepName = ''
				this.sleepShow = false
			},
			// 确认运动选择
			exerciseConfirm(e){
				this.exerciseName = e.value[0]
				this.exerciseShow = false
			},
			// 取消运动选择
			exerciseCancel(){
				this.exerciseName = ''
				this.exerciseShow = false
			},
			// 确认口味选择
			tasteConfirm(e){
				this.tasteName = e.value[0]
				this.tasteShow = false
			},
			// 取消口味选择
			tasteCancel(){
				this.tasteShow = false
				this.tasteName = ''
			},
			// 确认饮酒选择
			drinkConfirm(e){
				this.drinkName = e.value[0]
				this.drinkShow = false
			},
			// 取消饮酒选择
			drinkCancel(){
				this.drinkName = ''
				this.drinkShow = false
			},
			// 确认抽烟选择
			smokeConfirm(e){
				this.smokeName = e.value[0]
				this.smokeShow = false
			},
			// 取消抽烟选择
			smokeCancel(){
				this.smokeName = ''
				this.smokeShow = false
			},
			// 获取配置信息
			async getConfig() {
				let res = await this.$http.post(this.$api.config, {}, {
					token: true
				})
				let arr = []
				let arr1 = []
				let arr2 = []
				let arr3 = []
				arr = res.data.抽烟饮酒.split(',')
				this.smokeColumns.push(arr)
				arr1 = res.data.口味.split(',')
				this.tasteColumns.push(arr1)
				arr2 = res.data.运动习惯.split(',')
				this.exerciseColumn.push(arr2)
				arr3 = res.data.睡眠时间.split(',')
				this.sleepColumn.push(arr3)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-item1 {
		flex: 1;
		text-align: left;
		padding: 10rpx 20rpx;
	}
	.input-demo-item {
		padding: 10rpx 20rpx;
		background-color: #F7F7F7;
		border-radius: 10rpx;
	}
</style>